package com.lmb.todo.controller.interceptor;

import com.lmb.todo.utils.Constant;
import com.lmb.todo.utils.CookieUtil;
import com.lmb.todo.utils.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Lenovo
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        String tokenInCookie = CookieUtil.getCookieValue(Constant.TODOAUTH, req);
        boolean isTokenEffective = JwtUtil.checkToken(tokenInCookie, req);

        if (!isTokenEffective) {
            req.getRequestDispatcher("/api/user/error").forward(req, resp);
            return false;
        }

        return true;
    }

}
